<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <el-row :gutter="30">
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
            @input="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <!-- 添加商品按钮 -->
        <el-button type="primary" @click="goAddPage">添加商品</el-button>
      </el-row>
      <!-- tabel表格 -->
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="110px"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80px"></el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="100px">
          <template slot-scope="scope">{{scope.row.add_time|dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="deleteById(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表数据
      goodsList: [],
      //   获取商品列表时需要携带的参数 分页也需要
      queryInfo: {
        // 查询参数
        query: "",
        // 当前页码
        pagenum: 1,
        // 每条显示条数
        pagesize: 10
      },
      // 声明总条数
      total: 0
    };
  },
  created() {
    // 生命周期函数 一打开页面就调用此函数 获取列表数据
    this.getGoodsList();
  },
  methods: {
    //   获取商品列表数据
    async getGoodsList() {
      const { data } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      // console.log(data);
      // 给声明的商品列表赋值
      this.goodsList = data.data.goods;
      // 获取总条数 分页时会用
      this.total = data.data.total;
    },
    // 每页显示的条数发生变化时触发的函数 返回一个最新值
    handleSizeChange(newSize) {
      // 让每页现实的条数等于最新值
      this.queryInfo.pagesize = newSize;
      //   调用获取商品列表数据 让列表重新加载
      this.getGoodsList();
    },
    // 当前页码发生变化时触发的函数 返回一个最新值
    handleCurrentChange(newNum) {
      // 让每页现实的条数等于最新值
      this.queryInfo.pagenum = newNum;
      //   调用获取商品列表数据 让列表重新加载
      this.getGoodsList();
    },
    // 点击删除按钮 根据id删除商品数据
    async deleteById(row){
      const { data } = await this.$http.delete(`goods/${row.goods_id}`);
      // console.log(data);
      if(data.meta.status!==200){
      return this.$message.error(data.meta.msg)
      }
      this.$message.success(data.meta.msg)
      // 刷新页面
      this.getGoodsList()
    },
    // 点击添加商品按钮，跳转到add页面
    goAddPage(){
      this.$router.push('goods/add')
    }
  }
};
</script>

<style lang="less" scoped>
</style>